#include "led.h"

struct _led_t _led[] = {
	{GPIOC, GPIO_PIN_13},
};
//struct _led_t _led[] = {
//	{&htim4, TIM_CHANNEL_2},
//	{&htim4, TIM_CHANNEL_1},
//	{&htim8, TIM_CHANNEL_4},
//	{&htim8, TIM_CHANNEL_3},
//	{&htim8, TIM_CHANNEL_2},
//	{&htim8, TIM_CHANNEL_1},
//};
int led_numMax = sizeof(_led) / sizeof(struct _led_t);

/**
 * @brief  初始化led
 * @param  void
 * @retval void
 */
void led_init(void)
{
#ifdef LED_GPIO

#elif defined(LED_PWM)
	for (int i=0; i < led_numMax; i++) {
		HAL_TIM_PWM_Start(_led[i].htimx, _led[i].Channel);
	}
#endif
	
	Close_led();
}

/**
 * @brief  点亮led
 * @param  void
 * @retval void
 */
void led_on(unsigned char _led_ 
#ifdef LED_GPIO
) {
#elif
, unsigned short degree) {
#endif

	if (_led_ >= led_numMax) {
		return ;
	}
	
#ifdef LED_GPIO
	HAL_GPIO_WritePin(_led[_led_].GPIOx, _led[_led_].GPIO_PINx, LED_ON);
#elif defined(LED_PWM)
	__HAL_TIM_SetCompare(_led[_led_].htimx, _led[_led_].Channel, LED_MAX_PWM - degree);
#endif
}

/**
 * @brief  熄灭led
 * @param  void
 * @retval void
 */
void led_off(unsigned char _led_)
{
	if (_led_ >= led_numMax) {
		return ;
	}
	
#ifdef LED_GPIO
	HAL_GPIO_WritePin(_led[_led_].GPIOx, _led[_led_].GPIO_PINx, LED_OFF);
#elif defined(LED_PWM)
	__HAL_TIM_SetCompare(_led[_led_].htimx, _led[_led_].Channel, LED_MAX_PWM);
#endif
}

/**
 * @brief  led取反
 * @param  void
 * @retval void
 */
void led_not(unsigned char _led_)
{
	if (_led_ >= led_numMax) {
		return ;
	}
	
#ifdef LED_GPIO
	HAL_GPIO_TogglePin(_led[_led_].GPIOx, _led[_led_].GPIO_PINx);
#elif defined(LED_PWM)
	if (__HAL_TIM_GetCompare(_led[_led_].htimx, _led[_led_].Channel) == 0) {
		__HAL_TIM_SetCompare(_led[_led_].htimx, _led[_led_].Channel, LED_MAX_PWM);
	} else {
		__HAL_TIM_SetCompare(_led[_led_].htimx, _led[_led_].Channel, 0);
	}
#endif
}

/**
 * @brief  打开led
 * @param  void
 * @retval void
 */
void Open_led(void)
{
	for (int i=0; i < led_numMax; i++) {
#ifdef LED_GPIO
	led_on(i);
#elif defined(LED_PWM)
	led_on(i, LED_MAX_PWM);
#endif
		
	}
}

/**
 * @brief  关闭led
 * @param  void
 * @retval void
 */
void Close_led(void)
{
	for (int i=0; i < led_numMax; i++) {
		led_off(i);
	}
}

/**
 * @brief  开启灯的个数
 * @param  
 * @retval 无
 */
void led_onNum(unsigned char num)
{
	led_off(0);
	led_off(1);
	led_off(2);
	led_off(3);
	
	for (uint8_t i=0; i < num; i++) {
		led_on(i);
	}
}

/******************* (C) COPYRIGHT 2023 FUYEBING*****END OF FILE************/
